apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: ClusterIP
apiVersion: 指定使用的 Kubernetes API 版本,通常為 v1。kind: 表示資源類型,這裡是一個 Service。metadata: 包含有關 Service 的 raw data,包括名稱。spec: 指定了 Service 的規範,包括選擇器和 port 配置。selector:
  app: my-app
ports:
  - protocol: TCP
    port: 80
    targetPort: 8080
Kubernetes 支援多種 Service 類型,包括 ClusterIP、NodePort、LoadBalancer 和 ExternalName。 在這個範例中,使用了 ClusterIP 類型,這表示 Service 將獲得一個叢集內部的虛擬 IP 位址。
type: ClusterIP
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: ClusterIP